﻿using open_api_sdk_csharp.com.chinaums.open.api.internals.utils;
using open_api_sdk_csharp.response;
using System.Collections.Generic;
namespace open_api_sdk_csharp.com.chinaums.open.api
{
    public class OpenApiCache
    {
        private static readonly Dictionary<string, TokenResponse> Cache = new Dictionary<string, TokenResponse>();
        public static TokenResponse getCurrentToken(OpenApiContext context)
        {
            string appId = context.AppId;
            TokenResponse tokenResponse = null;
            if (Cache.ContainsKey(appId)) {
                tokenResponse = Cache[appId];
            }
            else
            {
                tokenResponse = TokenUtils.getToken(context);
                Cache[appId] = tokenResponse;
            }
            context.CurrentToken = tokenResponse;
            return tokenResponse;
        }
        public static void removeCache(string appId) {
           if(Cache.ContainsKey(appId)) Cache.Remove(appId);
        }
    }
}
